﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PRJ_LDW_Eutoliso.AccountAdmin.Evento
{
    public partial class Insert : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            

        }

        protected void btnInserir_Click(object sender, EventArgs e)
        {
            Guardar();
        }

        protected void btnCancelar_Click(object sender, EventArgs e)
        {
            Response.Redirect("Home.aspx");
        }


        private void Guardar()
        {
            tolisoASPNETDBEntities dbUser = new tolisoASPNETDBEntities();
            Users user = new Users();
            try
            {
                using (eutolisodbEntities1 context = new eutolisodbEntities1())
                {
                    evento ev = new evento();
                    //eventocategoria evc = new eventocategoria();
                    //cidade ci = new cidade();


                    var _categoria = context.eventocategoria.First(ec => ec.Nome == ddlCategoria.SelectedValue);

                    var _cidade = context.cidade.First(c => c.Nome == ddlCidade.SelectedValue);

                    //var _user = dbUser.Users.First(u => u.UserId == ev.Usuario);

                    //var _queryUser = from u in dbUser.Users join evt in context.evento on u.UserId equals evt.Usuario select u.UserId;

                    //var _query = (from u in dbUser.Users join _ev in context.evento on u.UserId equals _ev.Usuario 
                    //              select new 
                    //              {
                    //                _idAspUser = u.UserId,
                    //                _idEventoCat = _ev.Usuario
                    //              });

                    //var query = from u in dbUser.Users join _ev in context.evento on u.UserId equals _ev.Usuario select u.UserId;

                    //query = _idAspUser;
                
                    //
                    ev.cidade1 = _cidade;
                    ev.eventocategoria = _categoria;
                    //ev.Usuario = _user.UserId;
                    //ev.Usuario = 
                    
                    //adiciona nos campos 'textboxs'

                   if(FileUploadControl.HasFile)
                    {
                        try
                        {
                            if(FileUploadControl.PostedFile.ContentType == "~/Images/jpeg")
                            {
                                if(FileUploadControl.PostedFile.ContentLength < 102400)
                                {
                                    string filename = Path.GetDirectoryName(FileUploadControl.FileName);
                                    FileUploadControl.SaveAs(Server.MapPath("~/") + filename);                                    
                                    StatusLabel.Text = filename;
                                }
                                else
                                    StatusLabel.Text = "Upload status: The file has to be less than 100 kb!";
                            }
                            else
                                StatusLabel.Text = "Upload status: Only JPEG files are accepted!";
                        }
                        catch(Exception ex)
                        {
                            StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
                        }
                    }

                    ev.Nome = txtNome.Text;
                    ev.Datainicio = Convert.ToDateTime(txtDate1.Text);
                    ev.Datafim = Convert.ToDateTime(txtDate2.Text);
                    ev.Descricao = txtDescricao.Text;
                    ev.Video = txtxLinkVideo.Text;

                    ev.Imagem = "~/Images/" + FileUploadControl.FileName;

                    context.evento.Add(ev);
                    context.SaveChanges();

                    msgImgInserida.Visible = true;

                    Image1.ImageUrl = ev.Imagem;
                    Image1.Width = 200;
                    Image1.Height = 200;

                    lblMsg.Visible = true;

                }

            }
            catch (Exception)
            {
                
              
            }
        }
        
        
        protected void UploadButton_Click(object sender, EventArgs e)
        {
            if (FileUploadControl.HasFile)
            {
                try
                {
                    if (FileUploadControl.PostedFile.ContentType == "image/jpeg")
                    {
                        if (FileUploadControl.PostedFile.ContentLength < 102400)
                        {
                            string filename = Path.GetFileName(FileUploadControl.FileName);
                            FileUploadControl.SaveAs(Server.MapPath("~/") + filename);
                            StatusLabel.Text = "Upload status: File uploaded!";
                        }
                        else
                            StatusLabel.Text = "Upload status: The file has to be less than 100 kb!";
                    }
                    else
                        StatusLabel.Text = "Upload status: Only JPEG files are accepted!";
                }
                catch (Exception ex)
                {
                    StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
                }
            }
        }


  

       

    }
}